#include "Utils.h"
#include "SimpleAudioEngine.h"
#include "ResourceManager.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define SOUND_EXT ".ogg"
#else
#define SOUND_EXT ".wav"
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni/JniHelper.h>
#include <jni.h>
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "GKHWrapperCpp.h"
#include "CppObjcHelper.h"
#endif

void Utils::gameServicesSignIn(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/crazychicken/CrazyChicken", "gameServicesSignIn", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
#endif
}

void Utils::updateTopScoreLeaderboard(int score){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/crazychicken/CrazyChicken", "updateTopScoreLeaderboard", "(I)V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, score);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    GKHWrapperCpp gkh;
    gkh.submitScoreToCatagory(score, "1");
#endif
}

void Utils::showLeaderboards(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/crazychicken/CrazyChicken", "showLeaderboards", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    GKHWrapperCpp gkh;
    gkh.showLeaderBoard();
#endif
}

void Utils::rateApp(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/crazychicken/CrazyChicken", "rateApp", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    CppObjcHelper::getInstance()->openAppInAppstore();
#endif
}

void Utils::xcrypt(char* inputByte, unsigned long inputLength, char* encriptedByte, char* key){

	unsigned long keyLength = strlen(key);

	for(int i = 0; i < inputLength; i++){
		int keyIndex = i%keyLength;
		char sourceChar = inputByte[i];

		sourceChar = sourceChar^key[keyIndex];
		encriptedByte[i] = sourceChar;
	}
}

void Utils::playEffect(const char* soundName){
	char soundPath[64] = {0};
	sprintf(soundPath, SOUND_PATH"%s%s", soundName, SOUND_EXT);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(soundPath);
}

void Utils::preloadSound(const char* soundName){
	char soundPath[64] = {0};
	sprintf(soundPath, SOUND_PATH"%s%s", soundName, SOUND_EXT);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(soundPath);
}